package com.hinacom.pix.entity;

import org.apache.commons.lang.StringUtils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.UUID;
import java.lang.Integer;
/**
 * Created by fyu on 2016/9/1.
 */
public class PDQQueryParameter extends QueryParameter {
    private String tag;
    private String priority = "I";
    private int returnRecordCount = 1;
    private String returnUnits = "RD";
    public static String PID_PARAMETER_SPLIT = "[;]";
    public static String PID_PARAMETER_SPLIT_ORIGINAL = "~";
    private Patient userParameters;
    private String continuationPointer;

    public String getTag() {
        if (StringUtils.isEmpty(this.tag)) {
            String subTag = UUID.randomUUID().toString().substring(0, 8);
            subTag = String.valueOf(Long.parseLong(subTag, 16));
            this.tag = String.format("QRY%s", subTag);
        }
        return tag;
    }

    public void setTag(String tag) {
        this.tag = tag;
    }

    public String getPriority() {
        return priority;
    }

    // 返回查询条数
    public int getReturnRecordCount() {
        return returnRecordCount;
    }

    public void setReturnRecordCount(int returnRecordCount) {
        this.returnRecordCount = returnRecordCount;
    }

    public String getReturnUnits() {
        return returnUnits;
    }

    public void setReturnUnits(String returnUnits) {
        this.returnUnits = returnUnits;
    }

    public String generateParameter() {
        StringBuilder sb = new StringBuilder();
        if (this.userParameters != null) {
            // PID.3
            if (this.userParameters.getInternalIdentifier() != null) {
                PatientIdentifier patientIdentifier = this.userParameters.getInternalIdentifier();
                if (StringUtils.isNotEmpty(patientIdentifier.getId())) {
                    sb.append(String.format("@PID.3.1^%s", patientIdentifier.getId()));
                }

                if (patientIdentifier.getAssigningAuthority() != null) {
                    AssigningAuthority assigningAuthority = patientIdentifier.getAssigningAuthority();
                    if (StringUtils.isNotEmpty(assigningAuthority.getNamespaceId())) {
                        if (sb.length() != 0) {
                            sb.append(PID_PARAMETER_SPLIT);
                        }
                        sb.append(String.format("@PID.3.4.1^%s", assigningAuthority.getNamespaceId()));
                    }

                    if (StringUtils.isNotEmpty(assigningAuthority.getUniversalId())) {
                        if (sb.length() != 0) {
                            sb.append(PID_PARAMETER_SPLIT);
                        }
                        sb.append(String.format("@PID.3.4.2^%s", assigningAuthority.getUniversalId()));
                    }

                    if (StringUtils.isNotEmpty(assigningAuthority.getUniversalIDType())) {
                        if (sb.length() != 0) {
                            sb.append(PID_PARAMETER_SPLIT);
                        }
                        sb.append(String.format("@PID.3.4.3^%s", assigningAuthority.getUniversalIDType()));
                    }

                }

                if (StringUtils.isNotEmpty(patientIdentifier.getIdentifierTypeCode())) {
                    sb.append(String.format("@PID.3.5^%s", patientIdentifier.getIdentifierTypeCode()));
                }

                if (patientIdentifier.getAssigningFacility() != null) {
                    AssigningAuthority assigningAuthority = patientIdentifier.getAssigningFacility();
                    if (StringUtils.isNotEmpty(assigningAuthority.getNamespaceId())) {
                        if (sb.length() != 0) {
                            sb.append(PID_PARAMETER_SPLIT);
                        }
                        sb.append(String.format("@PID.3.6^%s", assigningAuthority.getNamespaceId()));
                    }
                }
            }

            // PID.5 Patient Name
            if (this.userParameters.getPatientName() != null) {
                PatientName patientName = this.userParameters.getPatientName();

                if (StringUtils.isNotEmpty(patientName.getLastName())) {
                    if (sb.length() != 0) {
                        sb.append(PID_PARAMETER_SPLIT);
                    }
                    sb.append(String.format("@PID.5.1.1^%s", patientName.getLastName()));
                }

                if (StringUtils.isNotEmpty(patientName.getFirstName())) {
                    if (sb.length() != 0) {
                        sb.append(PID_PARAMETER_SPLIT);
                    }
                    sb.append(String.format("@PID.5.2^%s", patientName.getFirstName()));
                }

                if (StringUtils.isNotEmpty(patientName.getMiddleName())) {
                    if (sb.length() != 0) {
                        sb.append(PID_PARAMETER_SPLIT);
                    }
                    sb.append(String.format("@PID.5.3^%s", patientName.getMiddleName()));
                }

                if (StringUtils.isNotEmpty(patientName.getSuffix())) {
                    if (sb.length() != 0) {
                        sb.append(PID_PARAMETER_SPLIT);
                    }
                    sb.append(String.format("@PID.5.4^%s", patientName.getSuffix()));
                }

                if (StringUtils.isNotEmpty(patientName.getPrefix())) {
                    if (sb.length() != 0) {
                        sb.append(PID_PARAMETER_SPLIT);
                    }
                    sb.append(String.format("@PID.5.5^%s", patientName.getPrefix()));
                }
            }

            // PID.6 Mother Name
            if (this.userParameters.getMotherName() != null) {
                PatientName patientName = this.userParameters.getMotherName();

                if (StringUtils.isNotEmpty(patientName.getLastName())) {
                    if (sb.length() != 0) {
                        sb.append(PID_PARAMETER_SPLIT);
                    }
                    sb.append(String.format("@PID.6.1^%s", patientName.getLastName()));
                }

                if (StringUtils.isNotEmpty(patientName.getFirstName())) {
                    if (sb.length() != 0) {
                        sb.append(PID_PARAMETER_SPLIT);
                    }
                    sb.append(String.format("@PID.6.2^%s", patientName.getFirstName()));
                }

                if (StringUtils.isNotEmpty(patientName.getMiddleName())) {
                    if (sb.length() != 0) {
                        sb.append(PID_PARAMETER_SPLIT);
                    }
                    sb.append(String.format("@PID.6.3^%s", patientName.getMiddleName()));
                }

                if (StringUtils.isNotEmpty(patientName.getSuffix())) {
                    if (sb.length() != 0) {
                        sb.append(PID_PARAMETER_SPLIT);
                    }
                    sb.append(String.format("@PID.6.4^%s", patientName.getSuffix()));
                }

                if (StringUtils.isNotEmpty(patientName.getPrefix())) {
                    if (sb.length() != 0) {
                        sb.append(PID_PARAMETER_SPLIT);
                    }
                    sb.append(String.format("@PID.6.5^%s", patientName.getPrefix()));
                }
            }

            // PID.7 Date/Time of Birth
            if (this.userParameters.getBirthDay() != null) {
                if (sb.length() != 0) {
                    sb.append(PID_PARAMETER_SPLIT);
                }

                SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
                String birthDay = df.format(this.userParameters.getBirthDay());
                sb.append(String.format("@PID.7.1^%s", birthDay));
            }

            // PID.8 Administrative Sex
            if (this.userParameters.getSex() != null) {
                if (sb.length() != 0) {
                    sb.append(PID_PARAMETER_SPLIT);
                }
                sb.append(String.format("@PID.8^%s", this.userParameters.getSex().getValue()));
            }

            // PID.11 Patient Address
            if (this.userParameters.getAddress() != null) {
                Address address = this.userParameters.getAddress();

                if (StringUtils.isNotEmpty(address.getStreetAddressLine1())) {
                    if (sb.length() != 0) {
                        sb.append(PID_PARAMETER_SPLIT);
                    }
                    sb.append(String.format("@PID.11.1.1^%s", address.getStreetAddressLine1()));
                }

                if (StringUtils.isNotEmpty(address.getStreetAddressLine2())) {
                    if (sb.length() != 0) {
                        sb.append(PID_PARAMETER_SPLIT);
                    }
                    sb.append(String.format("@PID.11.2^%s", address.getStreetAddressLine2()));
                }


                if (StringUtils.isNotEmpty(address.getCity())) {
                    if (sb.length() != 0) {
                        sb.append(PID_PARAMETER_SPLIT);
                    }
                    sb.append(String.format("@PID.11.3^%s", address.getCity()));
                }

                if (StringUtils.isNotEmpty(address.getState())) {
                    if (sb.length() != 0) {
                        sb.append(PID_PARAMETER_SPLIT);
                    }
                    sb.append(String.format("@PID.11.4^%s", address.getState()));
                }

                if (StringUtils.isNotEmpty(address.getPostalCode())) {
                    if (sb.length() != 0) {
                        sb.append(PID_PARAMETER_SPLIT);
                    }
                    sb.append(String.format("@PID.11.5^%s", address.getPostalCode()));
                }
                if (StringUtils.isNotEmpty(address.getCountry())) {
                    if (sb.length() != 0) {
                        sb.append(PID_PARAMETER_SPLIT);
                    }
                    sb.append(String.format("@PID.11.6^%s", address.getCountry()));
                }
            }

            // PID.13 Phone Number - Home
            if (this.userParameters.getHomePhoneNumber() != null) {
                PhoneNumber phoneNumber = this.userParameters.getHomePhoneNumber();

                if (StringUtils.isNotEmpty(phoneNumber.getCountryCode())) {
                    if (sb.length() != 0) {
                        sb.append(PID_PARAMETER_SPLIT);
                    }
                    sb.append(String.format("@PID.13.5^%s", phoneNumber.getCountryCode()));
                }

                if (StringUtils.isNotEmpty(phoneNumber.getAreaCode())) {
                    if (sb.length() != 0) {
                        sb.append(PID_PARAMETER_SPLIT);
                    }
                    sb.append(String.format("@PID.13.6^%s", phoneNumber.getAreaCode()));
                }

                if (StringUtils.isNotEmpty(phoneNumber.getNumber())) {
                    if (sb.length() != 0) {
                        sb.append(PID_PARAMETER_SPLIT);
                    }
                    sb.append(String.format("@PID.13.7^%s", phoneNumber.getNumber()));
                }

                if (StringUtils.isNotEmpty(phoneNumber.getExtension())) {
                    if (sb.length() != 0) {
                        sb.append(PID_PARAMETER_SPLIT);
                    }
                    sb.append(String.format("@PID.13.8^%s", phoneNumber.getExtension()));
                }

                if (StringUtils.isNotEmpty(phoneNumber.getNote())) {
                    if (sb.length() != 0) {
                        sb.append(PID_PARAMETER_SPLIT);
                    }
                    sb.append(String.format("@PID.13.9^%s", phoneNumber.getNote()));
                }
            }

            // PID.14 Phone Number - Home
            if (this.userParameters.getBusinessPhoneNumber() != null) {
                PhoneNumber phoneNumber = this.userParameters.getBusinessPhoneNumber();

                if (StringUtils.isNotEmpty(phoneNumber.getCountryCode())) {
                    if (sb.length() != 0) {
                        sb.append(PID_PARAMETER_SPLIT);
                    }
                    sb.append(String.format("@PID.14.5^%s", phoneNumber.getCountryCode()));
                }

                if (StringUtils.isNotEmpty(phoneNumber.getAreaCode())) {
                    if (sb.length() != 0) {
                        sb.append(PID_PARAMETER_SPLIT);
                    }
                    sb.append(String.format("@PID.14.6^%s", phoneNumber.getAreaCode()));
                }

                if (StringUtils.isNotEmpty(phoneNumber.getNumber())) {
                    if (sb.length() != 0) {
                        sb.append(PID_PARAMETER_SPLIT);
                    }
                    sb.append(String.format("@PID.14.7^%s", phoneNumber.getNumber()));
                }

                if (StringUtils.isNotEmpty(phoneNumber.getNumber())) {
                    if (sb.length() != 0) {
                        sb.append(PID_PARAMETER_SPLIT);
                    }
                    sb.append(String.format("@PID.14.8^%s", phoneNumber.getNumber()));
                }

                if (StringUtils.isNotEmpty(phoneNumber.getNote())) {
                    if (sb.length() != 0) {
                        sb.append(PID_PARAMETER_SPLIT);
                    }
                    sb.append(String.format("@PID.14.9^%s", phoneNumber.getNote()));
                }
            }


            // PID.18 Patient Account Number
            if (this.userParameters.getAccountNumber() != null) {
                PatientIdentifier patientIdentifier = this.userParameters.getAccountNumber();
                if (StringUtils.isNotEmpty(patientIdentifier.getId())) {
                    sb.append(String.format("@PID.18.1^%s", patientIdentifier.getId()));
                }

                if (patientIdentifier.getAssigningAuthority() != null) {
                    AssigningAuthority assigningAuthority = patientIdentifier.getAssigningAuthority();
                    if (StringUtils.isNotEmpty(assigningAuthority.getNamespaceId())) {
                        if (sb.length() != 0) {
                            sb.append(PID_PARAMETER_SPLIT);
                        }
                        sb.append(String.format("@PID.18.4^%s", assigningAuthority.getUniversalId()));
                    }
                }

                if (StringUtils.isNotEmpty(patientIdentifier.getIdentifierTypeCode())) {
                    sb.append(String.format("@PID.18.5^%s", patientIdentifier.getIdentifierTypeCode()));
                }

                if (patientIdentifier.getAssigningFacility() != null) {
                    AssigningAuthority assigningAuthority = patientIdentifier.getAssigningFacility();
                    if (StringUtils.isNotEmpty(assigningAuthority.getNamespaceId())) {
                        if (sb.length() != 0) {
                            sb.append(PID_PARAMETER_SPLIT);
                        }
                        sb.append(String.format("@PID.18.6^%s", assigningAuthority.getUniversalId()));
                    }
                }
            }


            // PID.19 SSN
            if (this.userParameters.getSsn() != null) {
                if (sb.length() != 0) {
                    sb.append(PID_PARAMETER_SPLIT);
                }
                sb.append(String.format("@PID.19.1^%s", this.userParameters.getSsn()));
            }


            // PID.20 Driver License
            if (this.userParameters.getDriverLicense() != null) {
                DriversLicense driverLicense = this.userParameters.getDriverLicense();
                if (driverLicense.getLicenseNumber() != null) {
                    if (sb.length() != 0) {
                        sb.append(PID_PARAMETER_SPLIT);
                    }
                    sb.append(String.format("@PID.20.1^%s", driverLicense.getLicenseNumber()));
                }

                if (driverLicense.getIssuingState() != null) {
                    if (sb.length() != 0) {
                        sb.append(PID_PARAMETER_SPLIT);
                    }
                    sb.append(String.format("@PID.20.2^%s", driverLicense.getIssuingState()));
                }

                if (driverLicense.getExpirationDate() != null) {
                    if (sb.length() != 0) {
                        sb.append(PID_PARAMETER_SPLIT);
                    }

                    SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
                    String expirationDate = df.format(driverLicense.getExpirationDate());
                    sb.append(String.format("@PID.20.3^%s", expirationDate));
                }


            }
        }

        String result = sb.toString();
        return result;
    }

    public Patient getUserParameters() {
        return userParameters;
    }

    public void setUserParameters(Patient userParameters) {
        this.userParameters = userParameters;
    }

    public String getContinuationPointer() {
        return continuationPointer;
    }

    public void setContinuationPointer(String continuationPointer) {
        this.continuationPointer = continuationPointer;
    }
}
